//
//  MYGAcademicCommentCell.swift
//  MYDoctor
//
//  Created by 小火科技 on 2024/6/2.
//

import UIKit
import QMUIKit

class MYGAcademicCommentCell: UITableViewCell {
    var likeBlock:(()->Void)?
    var answerBlock:(()->Void)?
    
    var model:MYGAcademicCommentCellModel? {
        didSet{
            guard let item = model else {
                return
            }
            avar.sd_setImage(with: URL(string: item.avar), placeholderImage: UIImage(named: "head"))
            nameLabel.text = item.name
            timeLabel.text = item.time
            commonLabel.text = item.content
            likeButton.setTitle("\(item.likeCount)", for: .normal)
            likeButton.setImage(item.isLike == 1 ? UIImage(named: "Community/like") : UIImage(named: "Community/dislike"), for: .normal)
            
            tableView.snp.updateConstraints { make in
                make.height.equalTo(item.cellHeight)
            }
            
            tableView.reloadData()
        }
    }
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        selectionStyle = .none
        configUI()
       
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    // MARK: - UI
    private func configUI() {
        contentView.backgroundColor = .white
        contentView.addSubview(avar)
        contentView.addSubview(nameLabel)
        contentView.addSubview(timeLabel)
        contentView.addSubview(commonLabel)
        contentView.addSubview(likeButton)
        contentView.addSubview(tableView)
        contentView.addSubview(answerButton)
        
        avar.snp.makeConstraints { make in
            make.left.top.equalToSuperview().inset(21.5)
            make.width.height.equalTo(42)
        }
        nameLabel.snp.makeConstraints { make in
            make.left.equalTo(avar.snp.right).offset(13)
            make.top.equalTo(avar)
        }
        timeLabel.snp.makeConstraints { make in
            make.left.equalTo(nameLabel)
            make.bottom.equalTo(avar)
        }
        likeButton.snp.makeConstraints { make in
            make.right.equalTo(-26.5)
            make.width.equalTo(30)
            make.height.equalTo(15)
            make.top.equalTo(timeLabel.snp.bottom).offset(13.5)
        }
        commonLabel.snp.makeConstraints { make in
            make.left.equalTo(nameLabel)
            make.top.equalTo(timeLabel.snp.bottom).offset(9.5)
            make.right.equalTo(likeButton.snp.left).offset(-5)
            make.height.equalTo(22.5)
        }
        tableView.snp.makeConstraints { make in
            make.left.equalTo(nameLabel)
            make.right.equalTo(likeButton)
            make.top.equalTo(commonLabel.snp.bottom).offset(14)
            make.height.equalTo(33)
        }
        answerButton.snp.makeConstraints { make in
            make.left.equalTo(nameLabel).offset(10)
            make.height.equalTo(15)
            make.width.equalTo(70)
            make.bottom.equalToSuperview().inset(10)
        }
        
    }
   
    private lazy var avar: UIImageView = {
        let icon = UIImageView()
        icon.layer.cornerRadius = 21
        icon.layer.masksToBounds = true
        return icon
    }()
    
    private lazy var nameLabel: UILabel = {
        let label = UILabel()
        label.font = .regular(14)
        label.textColor = .rgba(102, 102, 102, 1)
        return label
    }()
    
    private lazy var timeLabel: UILabel = {
        let label = UILabel()
        label.font = .regular(13)
        label.textColor = .rgba(204, 204, 204, 1)
        return label
    }()
    
    private lazy var commonLabel: UILabel = {
        let label = UILabel()
        label.font = .regular(16)
        label.textColor = .rgba(51, 51, 51, 1)
        label.numberOfLines = 0
        return label
    }()
    
    private lazy var likeButton: QMUIButton = {
        let button = QMUIButton(type: .custom)
        button.imagePosition = .left
        button.spacingBetweenImageAndTitle = 4.5
        button.titleLabel?.font = .regular(13)
        button.setTitleColor(.rgba(72, 68, 67), for: .normal)
        button.setTitle("0", for: .normal)
        button.setImage(UIImage(named: "Community/dislike"), for: .normal)
        button.addTarget(self, action: #selector(likeEvent), for: .touchUpInside)
        return button
    }()
    
    
    lazy var tableView: UITableView = {
        let control = UITableView.init(frame: .zero, style: .plain)
        control.showsVerticalScrollIndicator = false
        control.delegate = self
        control.dataSource = self
//        control.estimatedRowHeight = 170
//        control.rowHeight = UITableView.automaticDimension
        control.backgroundColor = .rgba(247, 247, 247, 1)
        control.separatorStyle = .none
        control.register(cellWithClass: MYGIntegerSubCell.self)
        return control
    }()
    
    private lazy var answerButton: QMUIButton = {
        let button = QMUIButton(type: .custom)
        button.imagePosition = .left
        button.spacingBetweenImageAndTitle = 4.5
        button.titleLabel?.font = .regular(12)
        button.setTitleColor(.rgba(152, 152, 152, 1), for: .normal)
        button.setTitle("我来回复", for: .normal)
        button.setImage(UIImage(named: "input_comment"), for: .normal)
        button.addTarget(self, action: #selector(toComment), for: .touchUpInside)
        button.contentMode = .left
        return button
    }()
    
    
    @objc func toComment(){
        answerBlock?()
    }

    @objc func likeEvent(){
        likeBlock?()
    }
}
extension MYGAcademicCommentCell:UITableViewDelegate,UITableViewDataSource {
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return model!.models.count
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        let item = model!.models[indexPath.row]
        let size = getStringSize(font: .regular(12), viewSize: CGSize(width: screenWidth - 205 , height: CGFLOAT_MAX), str: item.comment)
        return size.height + 11
     }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withClass: MYGIntegerSubCell.self)
        let item = model!.models[indexPath.row]
        cell.model = item
        return cell
    }
    
    
}
class MYGIntegerSubCell:UITableViewCell {
    
    var model: MYGTopicCommentModel?{
        didSet {
            guard let item = model else {return}
            nameLabel.text = "\(item.doctor.name):"
            titleLabel.text = item.comment
        }
    }
    
    // MARK: - init
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        selectionStyle = .none
        configUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    // MARK: - UI
    private func configUI() {
        contentView.backgroundColor = .rgba(247, 247, 247, 1)
        contentView.addSubview(nameLabel)
        contentView.addSubview(titleLabel)
        
        nameLabel.snp.makeConstraints { make in
            make.left.equalTo(14.5)
            make.top.equalToSuperview().offset(11)
            make.width.equalTo(70)
        }
        
        titleLabel.snp.makeConstraints { make in
            make.left.equalTo(nameLabel.snp.right).offset(2.5)
            make.right.equalTo(-15)
            make.top.equalTo(nameLabel)
        }
    }
    private lazy var nameLabel: UILabel = {
        let label = UILabel()
        label.font = .regular(12)
        label.textColor = .rgba(13, 12, 188, 1)
        return label
    }()
    private lazy var titleLabel: UILabel = {
        let label = UILabel()
        label.font = .regular(12)
        label.textColor = .rgba(84, 84, 84, 1)
        label.numberOfLines = 0
        return label
    }()

}
